package cn.ibizlab.odoo.core.odoo_survey.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Isurvey_survey;
import cn.ibizlab.odoo.core.odoo_survey.domain.Survey_survey;
import cn.ibizlab.odoo.core.odoo_survey.filter.Survey_surveySearchContext;
import cn.ibizlab.odoo.core.odoo_survey.service.ISurvey_surveyService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[问卷] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Survey_surveyServiceImpl implements ISurvey_surveyService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Isurvey_surveyClientService survey_surveyClientService;

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //survey_surveyClientService.removeBatch(survey_surveys) ;

    }

    @Override
    public Survey_survey get(Integer id) {
        Isurvey_survey clientModel = survey_surveyClientService.createModel();
        clientModel.setId(id);
        survey_surveyClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Survey_survey get(Survey_survey survey_survey) {
        Survey_survey retsurvey_survey = this.get(survey_survey.getId()) ;
    	BeanCopier copier=BeanCopier.create(retsurvey_survey.getClass(), survey_survey.getClass(), false);
        copier.copy(retsurvey_survey, survey_survey, null);
        return survey_survey ;
    }

    @Override
    public boolean create(Survey_survey survey_survey) {
        boolean bOk = false ;
        Isurvey_survey clientModel = this.convert2Model(survey_survey,null);
        survey_surveyClientService.create(clientModel) ;
        this.convert2Domain(clientModel,survey_survey);
        bOk = true ;

        return bOk ;
    }

    @Override
    public boolean update(Survey_survey survey_survey) {
        boolean bOk = false ;
        Isurvey_survey clientModel = this.convert2Model(survey_survey,null);
        survey_surveyClientService.update(clientModel) ;
        this.convert2Domain(clientModel,survey_survey) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Isurvey_survey clientModel = survey_surveyClientService.createModel();
        clientModel.setId(id);
        survey_surveyClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public void updateBatch(List<Survey_survey> survey_surveys, int batchSize) {

        //survey_surveyClientService.updateBatch(survey_surveys,batchSize) ;

    }

    @Override
    public void createBatch(List<Survey_survey> survey_surveys, int batchSize) {

                        //survey_surveyClientService.createBatch(survey_surveys,batchSize) ;

    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Survey_survey> searchDefault(Survey_surveySearchContext context) {
        List<Survey_survey> list = new ArrayList<Survey_survey>();
        Page<cn.ibizlab.odoo.core.client.model.Isurvey_survey> clientModelList = survey_surveyClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Isurvey_survey clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Survey_survey>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据EMail模板查询
     */
    @Override
    public Page<Survey_survey> selectByOdoo_email_template(Integer id){
        Survey_surveySearchContext context = new Survey_surveySearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Survey_survey> list = new ArrayList<Survey_survey>();
        Page<cn.ibizlab.odoo.core.client.model.Isurvey_survey> clientModelList = survey_surveyClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Isurvey_survey clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Survey_survey>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Survey_survey> selectByOdoo_create(Integer id){
        Survey_surveySearchContext context = new Survey_surveySearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Survey_survey> list = new ArrayList<Survey_survey>();
        Page<cn.ibizlab.odoo.core.client.model.Isurvey_survey> clientModelList = survey_surveyClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Isurvey_survey clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Survey_survey>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Survey_survey> selectByOdoo_write(Integer id){
        Survey_surveySearchContext context = new Survey_surveySearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Survey_survey> list = new ArrayList<Survey_survey>();
        Page<cn.ibizlab.odoo.core.client.model.Isurvey_survey> clientModelList = survey_surveyClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Isurvey_survey clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Survey_survey>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据调查阶段查询
     */
    @Override
    public Page<Survey_survey> selectByOdoo_stage(Integer id){
        Survey_surveySearchContext context = new Survey_surveySearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Survey_survey> list = new ArrayList<Survey_survey>();
        Page<cn.ibizlab.odoo.core.client.model.Isurvey_survey> clientModelList = survey_surveyClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Isurvey_survey clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Survey_survey>(list, context.getPageable(), clientModelList.getTotalElements());

    }



    public Isurvey_survey convert2Model(Survey_survey domain , Isurvey_survey model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = survey_surveyClientService.createModel() ;
        }

         if(domain.getMessage_partner_idsDirtyFlag())
            model.setMessage_partner_ids(domain.getMessage_partner_ids());
         if(domain.getCreate_dateDirtyFlag())
            model.setCreate_date(domain.getCreate_date());
         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.getWebsite_message_idsDirtyFlag())
            model.setWebsite_message_ids(domain.getWebsite_message_ids());
         if(domain.getUsers_can_go_backDirtyFlag())
            model.setUsers_can_go_back(domain.getUsers_can_go_back());
         if(domain.getMessage_channel_idsDirtyFlag())
            model.setMessage_channel_ids(domain.getMessage_channel_ids());
         if(domain.getMessage_unreadDirtyFlag())
            model.setMessage_unread(domain.getMessage_unread());
         if(domain.getMessage_has_errorDirtyFlag())
            model.setMessage_has_error(domain.getMessage_has_error());
         if(domain.getMessage_idsDirtyFlag())
            model.setMessage_ids(domain.getMessage_ids());
         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.getTot_start_surveyDirtyFlag())
            model.setTot_start_survey(domain.getTot_start_survey());
         if(domain.getResult_urlDirtyFlag())
            model.setResult_url(domain.getResult_url());
         if(domain.getPrint_urlDirtyFlag())
            model.setPrint_url(domain.getPrint_url());
         if(domain.getQuizz_modeDirtyFlag())
            model.setQuizz_mode(domain.getQuizz_mode());
         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getTot_comp_surveyDirtyFlag())
            model.setTot_comp_survey(domain.getTot_comp_survey());
         if(domain.getWrite_dateDirtyFlag())
            model.setWrite_date(domain.getWrite_date());
         if(domain.getMessage_unread_counterDirtyFlag())
            model.setMessage_unread_counter(domain.getMessage_unread_counter());
         if(domain.getActivity_summaryDirtyFlag())
            model.setActivity_summary(domain.getActivity_summary());
         if(domain.getPublic_url_htmlDirtyFlag())
            model.setPublic_url_html(domain.getPublic_url_html());
         if(domain.getActivity_type_idDirtyFlag())
            model.setActivity_type_id(domain.getActivity_type_id());
         if(domain.getMessage_main_attachment_idDirtyFlag())
            model.setMessage_main_attachment_id(domain.getMessage_main_attachment_id());
         if(domain.getMessage_follower_idsDirtyFlag())
            model.setMessage_follower_ids(domain.getMessage_follower_ids());
         if(domain.getMessage_is_followerDirtyFlag())
            model.setMessage_is_follower(domain.getMessage_is_follower());
         if(domain.getUser_input_idsDirtyFlag())
            model.setUser_input_ids(domain.getUser_input_ids());
         if(domain.getColorDirtyFlag())
            model.setColor(domain.getColor());
         if(domain.getPage_idsDirtyFlag())
            model.setPage_ids(domain.getPage_ids());
         if(domain.getAuth_requiredDirtyFlag())
            model.setAuth_required(domain.getAuth_required());
         if(domain.getDescriptionDirtyFlag())
            model.setDescription(domain.getDescription());
         if(domain.getMessage_needaction_counterDirtyFlag())
            model.setMessage_needaction_counter(domain.getMessage_needaction_counter());
         if(domain.getTot_sent_surveyDirtyFlag())
            model.setTot_sent_survey(domain.getTot_sent_survey());
         if(domain.getActivity_user_idDirtyFlag())
            model.setActivity_user_id(domain.getActivity_user_id());
         if(domain.getActivity_idsDirtyFlag())
            model.setActivity_ids(domain.getActivity_ids());
         if(domain.getMessage_attachment_countDirtyFlag())
            model.setMessage_attachment_count(domain.getMessage_attachment_count());
         if(domain.getDesignedDirtyFlag())
            model.setDesigned(domain.getDesigned());
         if(domain.getActivity_stateDirtyFlag())
            model.setActivity_state(domain.getActivity_state());
         if(domain.getMessage_has_error_counterDirtyFlag())
            model.setMessage_has_error_counter(domain.getMessage_has_error_counter());
         if(domain.getTitleDirtyFlag())
            model.setTitle(domain.getTitle());
         if(domain.getThank_you_messageDirtyFlag())
            model.setThank_you_message(domain.getThank_you_message());
         if(domain.getActivity_date_deadlineDirtyFlag())
            model.setActivity_date_deadline(domain.getActivity_date_deadline());
         if(domain.getPublic_urlDirtyFlag())
            model.setPublic_url(domain.getPublic_url());
         if(domain.getActiveDirtyFlag())
            model.setActive(domain.getActive());
         if(domain.getMessage_needactionDirtyFlag())
            model.setMessage_needaction(domain.getMessage_needaction());
         if(domain.getWrite_uid_textDirtyFlag())
            model.setWrite_uid_text(domain.getWrite_uid_text());
         if(domain.getIs_closedDirtyFlag())
            model.setIs_closed(domain.getIs_closed());
         if(domain.getCreate_uid_textDirtyFlag())
            model.setCreate_uid_text(domain.getCreate_uid_text());
         if(domain.getStage_id_textDirtyFlag())
            model.setStage_id_text(domain.getStage_id_text());
         if(domain.getEmail_template_id_textDirtyFlag())
            model.setEmail_template_id_text(domain.getEmail_template_id_text());
         if(domain.getStage_idDirtyFlag())
            model.setStage_id(domain.getStage_id());
         if(domain.getCreate_uidDirtyFlag())
            model.setCreate_uid(domain.getCreate_uid());
         if(domain.getEmail_template_idDirtyFlag())
            model.setEmail_template_id(domain.getEmail_template_id());
         if(domain.getWrite_uidDirtyFlag())
            model.setWrite_uid(domain.getWrite_uid());
        return model ;
    }

    public Survey_survey convert2Domain( Isurvey_survey model ,Survey_survey domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Survey_survey();
        }

        if(model.getMessage_partner_idsDirtyFlag())
            domain.setMessage_partner_ids(model.getMessage_partner_ids());
        if(model.getCreate_dateDirtyFlag())
            domain.setCreate_date(model.getCreate_date());
        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.getWebsite_message_idsDirtyFlag())
            domain.setWebsite_message_ids(model.getWebsite_message_ids());
        if(model.getUsers_can_go_backDirtyFlag())
            domain.setUsers_can_go_back(model.getUsers_can_go_back());
        if(model.getMessage_channel_idsDirtyFlag())
            domain.setMessage_channel_ids(model.getMessage_channel_ids());
        if(model.getMessage_unreadDirtyFlag())
            domain.setMessage_unread(model.getMessage_unread());
        if(model.getMessage_has_errorDirtyFlag())
            domain.setMessage_has_error(model.getMessage_has_error());
        if(model.getMessage_idsDirtyFlag())
            domain.setMessage_ids(model.getMessage_ids());
        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.getTot_start_surveyDirtyFlag())
            domain.setTot_start_survey(model.getTot_start_survey());
        if(model.getResult_urlDirtyFlag())
            domain.setResult_url(model.getResult_url());
        if(model.getPrint_urlDirtyFlag())
            domain.setPrint_url(model.getPrint_url());
        if(model.getQuizz_modeDirtyFlag())
            domain.setQuizz_mode(model.getQuizz_mode());
        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getTot_comp_surveyDirtyFlag())
            domain.setTot_comp_survey(model.getTot_comp_survey());
        if(model.getWrite_dateDirtyFlag())
            domain.setWrite_date(model.getWrite_date());
        if(model.getMessage_unread_counterDirtyFlag())
            domain.setMessage_unread_counter(model.getMessage_unread_counter());
        if(model.getActivity_summaryDirtyFlag())
            domain.setActivity_summary(model.getActivity_summary());
        if(model.getPublic_url_htmlDirtyFlag())
            domain.setPublic_url_html(model.getPublic_url_html());
        if(model.getActivity_type_idDirtyFlag())
            domain.setActivity_type_id(model.getActivity_type_id());
        if(model.getMessage_main_attachment_idDirtyFlag())
            domain.setMessage_main_attachment_id(model.getMessage_main_attachment_id());
        if(model.getMessage_follower_idsDirtyFlag())
            domain.setMessage_follower_ids(model.getMessage_follower_ids());
        if(model.getMessage_is_followerDirtyFlag())
            domain.setMessage_is_follower(model.getMessage_is_follower());
        if(model.getUser_input_idsDirtyFlag())
            domain.setUser_input_ids(model.getUser_input_ids());
        if(model.getColorDirtyFlag())
            domain.setColor(model.getColor());
        if(model.getPage_idsDirtyFlag())
            domain.setPage_ids(model.getPage_ids());
        if(model.getAuth_requiredDirtyFlag())
            domain.setAuth_required(model.getAuth_required());
        if(model.getDescriptionDirtyFlag())
            domain.setDescription(model.getDescription());
        if(model.getMessage_needaction_counterDirtyFlag())
            domain.setMessage_needaction_counter(model.getMessage_needaction_counter());
        if(model.getTot_sent_surveyDirtyFlag())
            domain.setTot_sent_survey(model.getTot_sent_survey());
        if(model.getActivity_user_idDirtyFlag())
            domain.setActivity_user_id(model.getActivity_user_id());
        if(model.getActivity_idsDirtyFlag())
            domain.setActivity_ids(model.getActivity_ids());
        if(model.getMessage_attachment_countDirtyFlag())
            domain.setMessage_attachment_count(model.getMessage_attachment_count());
        if(model.getDesignedDirtyFlag())
            domain.setDesigned(model.getDesigned());
        if(model.getActivity_stateDirtyFlag())
            domain.setActivity_state(model.getActivity_state());
        if(model.getMessage_has_error_counterDirtyFlag())
            domain.setMessage_has_error_counter(model.getMessage_has_error_counter());
        if(model.getTitleDirtyFlag())
            domain.setTitle(model.getTitle());
        if(model.getThank_you_messageDirtyFlag())
            domain.setThank_you_message(model.getThank_you_message());
        if(model.getActivity_date_deadlineDirtyFlag())
            domain.setActivity_date_deadline(model.getActivity_date_deadline());
        if(model.getPublic_urlDirtyFlag())
            domain.setPublic_url(model.getPublic_url());
        if(model.getActiveDirtyFlag())
            domain.setActive(model.getActive());
        if(model.getMessage_needactionDirtyFlag())
            domain.setMessage_needaction(model.getMessage_needaction());
        if(model.getWrite_uid_textDirtyFlag())
            domain.setWrite_uid_text(model.getWrite_uid_text());
        if(model.getIs_closedDirtyFlag())
            domain.setIs_closed(model.getIs_closed());
        if(model.getCreate_uid_textDirtyFlag())
            domain.setCreate_uid_text(model.getCreate_uid_text());
        if(model.getStage_id_textDirtyFlag())
            domain.setStage_id_text(model.getStage_id_text());
        if(model.getEmail_template_id_textDirtyFlag())
            domain.setEmail_template_id_text(model.getEmail_template_id_text());
        if(model.getStage_idDirtyFlag())
            domain.setStage_id(model.getStage_id());
        if(model.getCreate_uidDirtyFlag())
            domain.setCreate_uid(model.getCreate_uid());
        if(model.getEmail_template_idDirtyFlag())
            domain.setEmail_template_id(model.getEmail_template_id());
        if(model.getWrite_uidDirtyFlag())
            domain.setWrite_uid(model.getWrite_uid());
        return domain ;
    }


}
